package com.smsc.headend.task.engine.handler.impl.collection;

import cn.hutool.core.convert.Convert;
import cn.hutool.json.JSONUtil;
import com.smsc.headend.common.enums.errorcode.TaskHandlerErrorCode;
import com.smsc.headend.module.protocol.DeviceData;
import com.smsc.headend.module.protocol.cosem.bo.CosemData;
import com.smsc.headend.module.task.dto.AtomicTask;
import com.smsc.headend.module.task.dto.Task;
import com.smsc.headend.task.engine.exception.UDISTaskHandlerException;
import com.smsc.headend.task.engine.handler.TaskHandler;
import com.smsc.headend.task.engine.handler.impl.collection.builder.BuildScheduleReadResultFactory;
import com.smsc.headend.task.engine.handler.impl.collection.builder.ScheduleReadBuilder;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationTargetException;
import java.util.Map;
import java.util.Optional;

/**
 * 巡检任务
 * zhouyufeng
 *
 * 抄读相位需要发送到DCU而非表
 */
@Slf4j
@Component
public class ReadScheduleReadBufferHandler implements TaskHandler {

    @Autowired
    BuildScheduleReadResultFactory buildScheduleReadResultFactory;
    @SneakyThrows
    @Override
    public int handle(Task task, AtomicTask atomicTask, DeviceData deviceData, String actionResult) throws UDISTaskHandlerException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {

        if (!(deviceData instanceof CosemData)) {
            throw new UDISTaskHandlerException(TaskHandlerErrorCode.DEVICE_DATA_TYPE_NOT_SUPPORT.toString());
        }

//        CosemData cosemData = (CosemData) deviceData;
//        if (CollectionUtils.isEmpty(cosemData.getChild())) {
//            return 1;
//        }
        log.info("ReadScheduleReadBufferHandler, device data:" + JSONUtil.toJsonStr(deviceData) +
                ",task data:" + JSONUtil.toJsonStr(task) + ",atomicTask:" + JSONUtil.toJsonStr(atomicTask)
        + ",actionResult:" + actionResult);

        Map<String, Object> param = getParameterMap(task,atomicTask);
        Boolean dcuDataItemFlag = null == task.getMeterId();
        Long dataItemId = null;
        Object dataItemIdObj =  param.get("dataItemId");
        if (Optional.ofNullable(dataItemIdObj).isPresent()){
            dataItemId = Convert.toLong(dataItemIdObj);
        }


        if (dcuDataItemFlag){
            ScheduleReadBuilder scheduleDcuReadBuilder = buildScheduleReadResultFactory.buildScheduleReadResult(dataItemId);
            scheduleDcuReadBuilder.handleScheduleReadResult(task, atomicTask, deviceData, actionResult, dcuDataItemFlag,param);
        }else {
            ScheduleReadBuilder scheduleReadBuilder = buildScheduleReadResultFactory.buildScheduleReadResult(dataItemId);
            scheduleReadBuilder.handleScheduleReadResult(task, atomicTask, deviceData, actionResult, dcuDataItemFlag,param);
        }
        return 1;
    }

}
